# 项目：python_project
# 文件：Demo3
# 作者：WuTong
# 日期：2025/5/23,14:25
# coding:utf-8
import json

class Util:
    @classmethod
    def read_csv(cls,path):
        with open(path,encoding='utf8') as file:
            contents = file.readlines()
            li = []
        for content in contents:
            if not content.startswith('# ') and content != '\n' :
                if '# ' in content.strip():
                    item = content.strip().split('# ')[0].strip()
                else:
                    item = content.strip()
                li.append(item)
        return li

    @classmethod
    def read_json(cls,path):
        with open(path,encoding='utf8') as file:
            contents = json.load(file)
        return contents

    @classmethod
    def get_conn(cls):
        """
        链接数据库
        :return: 无
        """
        import pymysql
        db_info = cls.read_json('conf.json')['appdb']
        return pymysql.connect(**db_info, charset='utf8')

    @classmethod
    def query(cls, sql):
        conn = cls.get_conn()
        cursor = conn.cursor()
        cursor.execute(sql)
        result = cursor.fetchall()
        cursor.close()
        conn.close()
        return result


if __name__ == '__main__':
    pass
